package com.ruoyi.tester.executor.expression;

import com.ruoyi.tester.executor.ExpressionContext;
import org.springframework.stereotype.Component;

/**
 * @author SongJun
 * @description TODO:
 * @date 2021/5/31 17:08
 */

@Component
public class ExpSubString implements ExpressionAnalysis{
    @Override
    public String getExpressionName() {
        return "subString";
    }

    @Override
    public String getDescription() {
        return "subString(x, y) 返回从当前文本类型的上下文中截取序号x到y之间的文本;";
    }

    @Override
    public Object analysis(ExpressionContext context, String... param) {
        try{
            String str = String.valueOf(context.getContext());
            return str.substring(Integer.valueOf(param[0]), Integer.valueOf(param[1]));
        } catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("解析subString("+param[0]+","+param[1]+")时发生异常");
        }
    }
}
